package servlet;

import bean.Book;
import dao.BookDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "FindBookServlet", value = "/FindBookServlet")
public class FindBookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;
        out = response.getWriter();
        int id = Integer.parseInt(request.getParameter("id"));
        BookDao bookDao = new BookDao();
        try {
            if (bookDao.findBook(id)!=null){
                Book newBook = bookDao.findBook(id);
//                out.println("<h1>本书信息为</h1>\n");
//                out.println(newBook.toTable());
                request.setAttribute("book",newBook);
                request.getRequestDispatcher("queryBook.jsp").forward(request,response);


            }else {
                out.println("查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
